package com.stylefeng.guns.core.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;


public class ParseXml {
	@SuppressWarnings("unchecked")
	public static String getJsonTxt(){
		String jsonTxt="";
	    List<String> list=new ArrayList<String>();
	    list.add("广州");
	    list.add("上海");
	    list.add("厦门");
	    list.add("兰州");
	    list.add("西安");
	    Map<String, List> map=new HashMap<String, List>();
	    map.put("city", list);

	    jsonTxt=JSONObject.toJSON(map).toString();
		return jsonTxt;		
	}
	public static String getXmlStrFromStream(){
		String xmlStr="";	
		String value="";
		StringBuilder sb=new StringBuilder();
	    InputStream ins=ParseXml.class.getClassLoader().getResourceAsStream("com/jjkj/toolkit/Citys.xml");
		Reader reader=new InputStreamReader(ins);
		BufferedReader br=new BufferedReader(reader);
		try {
			while((value=br.readLine())!=null){			
				sb.append(value);
				//System.out.println(value);
			}	
			xmlStr=sb.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return xmlStr;		
	}
	public static List<String> parsexml(String xmltxt) throws IOException{
		List<String> list=null;
		try {
			XmlPullParserFactory xmlFactory =XmlPullParserFactory.newInstance();
			xmlFactory.setNamespaceAware(true); 
			XmlPullParser xmlparse=xmlFactory.newPullParser();
			xmlparse.setInput(new StringReader(xmltxt));
			int eventType=xmlparse.getEventType();
			String value="";
			while(eventType!=XmlPullParser.END_DOCUMENT){				
				switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList<String>();
						break;
	
					case XmlPullParser.START_TAG:
						if("name".equals(xmlparse.getName())){
							value=xmlparse.nextText();
						}
						break;
					case XmlPullParser.END_TAG:		
						list.add(value);
					    break;
				}				
				xmlparse.next();
				eventType=xmlparse.getEventType();
			}

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public static String map2Xmlstring(Map<String,Object> map){  
        StringBuffer sb = new StringBuffer("");  
        sb.append("<xml>");  
        Set<String> set = map.keySet();  
        for(Iterator<String> it=set.iterator(); it.hasNext();){  
            String key = it.next();  
            Object value = map.get(key);  
            sb.append("<").append(key).append(">");  
            sb.append(value);  
            sb.append("</").append(key).append(">");  
        }  
        sb.append("</xml>");  
        return sb.toString();  
    }  
    /**  
     * Xml string转换成Map  
     * @param xmlStr  
     * @return  
     */  
    public static Map<String,Object> xmlString2Map(String xmlStr){  
        Map<String,Object> map = new HashMap<String,Object>();  
        Document doc;  
        try {  
            doc = DocumentHelper.parseText(xmlStr);  
            Element el = doc.getRootElement();  
            map = recGetXmlElementValue(el,map);  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
        return map;  
    }  
    /**  
     * 循环解析xml  
     * @param ele  
     * @param map  
     * @return  
     */  
    @SuppressWarnings({ "unchecked" })  
    private static Map<String, Object> recGetXmlElementValue(Element ele, Map<String, Object> map){  
        List<Element> eleList = ele.elements();  
        if (eleList.size() == 0) {  
            map.put(ele.getName(), ele.getTextTrim());  
            return map;  
        } else {  
            for (Iterator<Element> iter = eleList.iterator(); iter.hasNext();) {  
                Element innerEle = iter.next();  
                recGetXmlElementValue(innerEle, map);  
            }  
            return map;  
        }  
    }  
	public static void main(String[] args) {

		System.out.println(getJsonTxt());
	}

}
